package fr.oz.mobile.common.horloge
{
	import caurina.transitions.Tweener;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.Timer;
	import fr.digitas.flowearth.text.styles.styleManager;
	import fr.oz.core.events.ModuleEvent;
	import fr.oz.core.utils.Utils;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class Horloge extends Sprite
	{
		
		private var tfHour:TextField;
		private var tfMinut:TextField;
		private var tfHourTW:TextField;
		private var tfMinutTW:TextField;
		
		private var timer:Timer = new Timer(10000);
		private var date:Date;
		
		private var previewWakeUp:AlarmClockPreview;
		
		public function Horloge() 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			
			tfHour = new TextField();
			tfMinut = new TextField();
			tfHourTW = new TextField();
			tfMinutTW = new TextField();
			
			previewWakeUp = new AlarmClockPreview();
			
			styleManager.applyFormat(tfHour, ".home_horloge_hour");
			styleManager.applyFormat(tfHourTW, ".home_horloge_hour");
			styleManager.applyFormat(tfMinut, ".home_horloge_minut");
			styleManager.applyFormat(tfMinutTW, ".home_horloge_minut");
			
			tfHour.autoSize = TextFieldAutoSize.RIGHT;
			tfHourTW.autoSize = TextFieldAutoSize.RIGHT;
			addChild(tfHour);
			addChild(tfMinut);
			addChild(tfHourTW);
			addChild(tfMinutTW);
			
			updateHour();
			
			tfHourTW.x = -tfHourTW.width + 2;
			tfMinutTW.x = -2;
			
			previewWakeUp.update();
			addChild(previewWakeUp);
			previewWakeUp.y = tfHourTW.height + 20;
			previewWakeUp.addEventListener(MouseEvent.CLICK, handleShowWakeUpSetting);
		}
		
		private function handleShowWakeUpSetting(e:MouseEvent):void 
		{
			dispatchEvent(new ModuleEvent(ModuleEvent.MODULE_SELECTED, "wakeUpProfile", true));
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			timer.addEventListener(TimerEvent.TIMER, handleChangeTime);
			timer.start();
		}
		
		private function dispose(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, dispose);
			
			timer.removeEventListener(TimerEvent.TIMER, handleChangeTime);
			timer.stop();
		}
		
		private function updateHour():void
		{
			date = new Date();
			
			tfHourTW.text = Utils.addZero("" + date.getHours());
			tfMinutTW.text = Utils.addZero("" + date.getMinutes());
			tfHourTW.alpha = 0;
			tfMinutTW.alpha = 0;
			
			Tweener.addTween(tfHourTW, { alpha:1, time:1.1, onComplete:restoreStates} );
			Tweener.addTween(tfMinutTW, { alpha:1, time:1} );
			Tweener.addTween([tfMinut, tfHour], { alpha:0, time:1} );
		}
		
		private function restoreStates():void
		{
			trace("restore States");
			tfHour.text = tfHourTW.text;
			tfMinut.text = tfMinutTW.text;
			//tfHourTW.alpha = tfMinutTW.alpha = 0;
			Tweener.addTween(tfHourTW, { alpha:0 } );
			Tweener.addTween(tfMinutTW, { alpha:0 } );
			Tweener.addTween([tfMinut, tfHour], { alpha:1} );
			tfHour.x = -tfHour.width + 2;
			tfMinut.x = -2;
			trace("tfHour.text " + tfHour.text);
			trace("tfMinut.text " + tfMinut.text);
		}
		
		private function handleChangeTime(e:TimerEvent):void 
		{
			updateHour();
		}
		
	}

}